package com.dh.blog.controllers.admin;

import java.util.List;
import java.util.Properties;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dh.blog.service.ClassifyService;
import com.dh.blog.vo.ClassifyVo;
import com.ewei.web.common.json.resolver.JsonModel;
import com.ewei.web.common.response.ResponseVO;
import com.ewei.web.common.validator.SaveGroup;

/**
 * 类别控制器(管理员)
 * @author David
 *
 */
@RequestMapping("/admin/article/classify")
@Controller
public class AdminClassifyController {
	private final String identify = "694af96a64e711e6961d00ff8a62d83e";
	@Resource
	private Properties config;
	@Resource(name="classifyService")
	private ClassifyService classifyService;
	/**
	 * 增加类别 
	 */
	@RequestMapping(value="",method=RequestMethod.POST)
	@ResponseBody
	public Object addClassfinition(@JsonModel @Validated(value=SaveGroup.class) 
				ClassifyVo classify,BindingResult result){
		if(result.hasErrors()){
			return new ResponseVO().resolveError(result);
		}
		//TODO 从Cookie信息中获得blogId=userId
		//防止重复
		if(null != classifyService.getByName(
				classify.getName())){
			return new ResponseVO().error("301",config.get("301"));
		}
		classifyService.doCreateOne(classify);
		return new ResponseVO();
	}
	@RequestMapping(value="",method=RequestMethod.GET)
	@ResponseBody
	public Object find(){
		List<ClassifyVo> list = classifyService.listAllByBlog(identify);
		return new ResponseVO().setDatas(list);
	}
}
